#include "log_fac.h"
#include "log_console_output.h"
#include "log_file_output.h"
#include "xlog_format.h"
#include "xconfig.h"
#include "xml_format.h"

#include <iostream>

#define LOGFILE "log.txt"

using namespace std;

void LogFac::Init(const std::string& con_file)
{ 
	

	XConfig conf;
	bool re = conf.Read(con_file);
	string log_type = "console";
	string log_file = LOGFILE;
	string log_level = "debug";
	string log_fmt = "default";
	if(re)
	{ 
		if(!conf.Get("log_type").empty()) 
			log_type = conf.Get("log_type");
		if (!conf.Get("log_file").empty()) 
			log_file = conf.Get("log_file");
		if (!conf.Get("log_level").empty()) 
			log_level = conf.Get("log_level");
		if (!conf.Get("log_fmt").empty())
			log_fmt = conf.Get("log_fmt");
	}

	if (log_fmt == "xml") {
		logger_.SetFormat(new XmlFormat());
	}
	else {
		logger_.SetFormat(new XLogFormat());
	}

	if (log_level == "info")
	{
		logger_.SetLevel(XLog::INFO);
	}
	else if (log_level == "error")
	{
		logger_.SetLevel(XLog::ERROR);
	}
	else if (log_level == "fatal")
	{ 
		logger_.SetLevel(XLog::FATAL);
	}

	if (log_type == "file")
	{
		auto fout = new LogFileOutput();
		if (!fout->Open(log_file))
		{
			cerr << "open file failed" << log_file << endl;
		}
		logger_.SetOutput(fout);
	}
	else {
		logger_.SetOutput(new LogConsoleOutput());
	}
	
	
}